package com.ybwh.springboot1.conf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
	@Autowired
    private RestTemplateBuilder builder;

    /**
     * 使用RestTemplateBuilder来实例化RestTemplate对象，spring默认已经注入了RestTemplateBuilder实例
     *
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        return builder.build();
    }


//	@Bean
//	public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
//		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
//		// 单位为ms
//		factory.setReadTimeout(5000);
//		// 单位为ms
//		factory.setConnectTimeout(5000);
//		return factory;
//	}
//
//	@Bean
//	@ConditionalOnMissingBean({RestTemplate.class})
//	public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
//		RestTemplate restTemplate = new RestTemplate(factory);
//
//		//换上fastjson
//		List<HttpMessageConverter<?>> httpMessageConverterList= restTemplate.getMessageConverters();
//		Iterator<HttpMessageConverter<?>> iterator=httpMessageConverterList.iterator();
//		if(iterator.hasNext()){
//			HttpMessageConverter<?> converter=iterator.next();
//			//原有的String是ISO-8859-1编码 去掉
//			if(converter instanceof StringHttpMessageConverter){
//				iterator.remove();
//			}
//
//			//由于系统中默认有jackson 在转换json时自动会启用  但是我们不想使用它 可以直接移除或者将fastjson放在首位
//            if(converter instanceof GsonHttpMessageConverter || converter instanceof MappingJackson2HttpMessageConverter){
//                iterator.remove();
//            }
//
//		}
//		httpMessageConverterList.add(new StringHttpMessageConverter(Charset.forName("utf-8")));
//		FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//		FastJsonConfig fastJsonConfig = new FastJsonConfig();
//		fastJsonConfig.setSerializerFeatures(
//				SerializerFeature.WriteMapNullValue,
//				SerializerFeature.WriteNullStringAsEmpty,
//				SerializerFeature.WriteNullListAsEmpty,
//				SerializerFeature.DisableCircularReferenceDetect);
//
//		fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
//		httpMessageConverterList.add(0,fastJsonHttpMessageConverter);
//
//		return restTemplate;
//
//	}

}
